package com.linkage.controller.background;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.linkage.interceptor.NeedAdminLogin;
import com.linkage.interceptor.NeedLogin;
import com.linkage.pojo.commodity.Category;
import com.linkage.service.background.OrderManageService;

@Controller
@RequestMapping("/admin/order")

public class OrderManageController {

    @Autowired
    OrderManageService orderManageService;

    @NeedAdminLogin
    @RequestMapping(value = "/orderlist", method = RequestMethod.GET)
    public String list(Model model) {
        List<Map<String, Object>> orders = this.orderManageService.listAllOrderDetails();
        model.addAttribute("orders",orders );
        Integer page = 0;
        model.addAttribute("page",page);

        return "background/order/orderlist";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/orderlist/{status}", method = RequestMethod.GET)
    public String orderListByStatus(@PathVariable("status") Integer status,Model model) {
      
        List<Map<String, Object>> orders=this.orderManageService.listOrderByStatus(status);
        model.addAttribute("orders",orders );
        model.addAttribute("page", status);
        return "background/order/orderlist";
    }
    @NeedAdminLogin
    @RequestMapping(value = "/orderlist/orderdetail/{singleOrderId}", method = RequestMethod.GET)
    public String orderDetail(@PathVariable("singleOrderId") Integer singleOrderId,Model model) {
      
        Map<String, Object>  orderDetail = this.orderManageService.getOrderDetailById(singleOrderId);
        model.addAttribute("order",orderDetail );
        return "background/order/orderdetail";
    }
    @NeedAdminLogin
    @RequestMapping(value = "/changestatus", method = RequestMethod.POST)
    public String addCategory(@RequestParam("singleOrderId") Integer singleOrderId,@RequestParam("statusId")String statusId) {
        this.orderManageService.changeOrderStatus(Integer.valueOf(statusId), singleOrderId);
        String url = "redirect:/admin/order/orderlist/orderdetail/"+singleOrderId.toString();
        return url;
    }
    
    
    
}
